package org.musync;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

import org.ravey.plugin.PluginManager;

/**
 * Create a new instance of the musync engine. This is the class that manages
 * pretty much all musync operations on the highest level.
 */

public class MusyncEngine {
	
	private PluginManager pm;
	private Connection con;
	private int KernelVersion = 1;
	private String ApplicationName = "Musync";
	private String ManifestPath = "plugins.xml";
	
	//access to properties handler by other classes is permitted!
	public Properties conf;
	

	public MusyncEngine() {
		conf = null;
		//Start up sequence for the musync engine begins with loading basic config
		LoadConfig();
		
		//Next we can load the database
		LoadDatabase();
		
		//next we load all of the plugins into the engine
		try {
			LoadPlugins();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}
	
	/**
	 * Load all plugins defined in the configuration file into the system
	 * @throws Exception
	 */
	private void LoadPlugins() throws Exception{
		
		if(conf == null)
		{
			throw new Exception("There must be some configuration values for this to work.");
		}
		
		
		//initiate the plugin manager
		pm = new PluginManager(KernelVersion, ApplicationName);
		
		File f = new File(ManifestPath);
		//if a valid manifest exists, load the plugins
		if(f.isFile()){		
			pm.LoadPlugins(ManifestPath);
		}
		//otherwise, create a new manifest at the given path.
		else
		{
			pm.CreateManifest(ManifestPath);
		}
			
		
	}
	
	/**
	 * Load configuration from an external file and import to the application
	 */
	private void LoadConfig(){
		//for now we hardwire in config.xml. However, later, we can change this
		try {
			FileInputStream config = new FileInputStream("config.properties");
			//create properties object
			conf = new Properties();
			//load the properties into the system
			conf.load(config);
			//close the file used to store properties
			config.close();
			
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
	}

	/**
	 * Function to load the musync database on initial startup
	 */
	private boolean LoadDatabase(){
		
		try {
			Class.forName(conf.getProperty("database.jdbc_driver"));
			con = DriverManager.getConnection("jdbc:" + conf.getProperty("database.connect_uri"),
					conf.getProperty("database.user"),
					conf.getProperty("database.passwd")
					);
		
			if(!con.isClosed()){
				return true;
			}else{
				return false;
			}
		
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return false;
		
	}


}
